package com.demo2do.boyle.backend.web.controller.advertisement;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.demo2do.boyle.backend.service.advertisement.AdvertisementService;
import com.demo2do.boyle.common.entity.advertisement.Advertisement;
import com.demo2do.boyle.common.entity.advertisement.Status;
import com.demo2do.boyle.common.entity.system.Principal;
import com.demo2do.core.entity.Result;
import com.demo2do.core.utils.JsonUtils;
import com.demo2do.core.web.interceptor.MenuSetting;
import com.demo2do.core.web.resolver.Page;
import com.demo2do.core.web.resolver.Secure;

/**
 * 
 * @author mohanlan leo
 *
 */
@Controller
@RequestMapping("/advertisement-audits")
@MenuSetting("menu-advertisement")
public class AdvertisementAuditController {
	
	@Autowired
	private AdvertisementService advertisementService;	
    
    /**
     * list all auditing advertisement list
     * 
     * @return 
     */
    @RequestMapping(value= "")
    @MenuSetting("submenu-advertisement-audit")
    public ModelAndView list(Page page) {
    	
        ModelAndView modelAndView = new ModelAndView("advertisement/audit/advertisement-audit-list");
        
        List<Advertisement> advertisements = advertisementService.list(Status.AUDITING, page);
        
        int count =  advertisementService.count(Status.AUDITING);
    	
        modelAndView.addObject("advertisements", advertisements);
        modelAndView.addObject("count", count);
        
        return modelAndView;
    }
    
    /**
     * view the audit advertisement detail
     * 
     * @param id
     * @return
     */
    @RequestMapping("/{id}")
    @MenuSetting("submenu-advertisement-audit")
    public ModelAndView view(@PathVariable("id") Integer id, Page page) {
        ModelAndView modelAndView = new ModelAndView("advertisement/audit/advertisement-audit-view");
        
        Advertisement advertisement = advertisementService.load(Advertisement.class, id);
        
        modelAndView.addObject("advertisement", advertisement);
        
        return modelAndView;
    }
    
    /**
     * display the audit panel
     * 
     * @param id
     * @return the view to confirm the submit
     */
    @RequestMapping("/{id}/pass")
    public ModelAndView pass(@PathVariable("id") int id) {
    	ModelAndView modelAndView = new ModelAndView("advertisement/audit/advertisement-audit-pass");
		modelAndView.addObject("id", id);
		return modelAndView;
    }
    
    /**
     * do audit
     * 
     * @param id
     * @return the audit result with message
     */
    @RequestMapping(value ="/{id}/pass", method = RequestMethod.PATCH )
    @MenuSetting("submenu-advertisement-audit")
    public @ResponseBody String onPass(@PathVariable("id") int id, @Secure Principal principal) {
    	Result result = new Result();
    	
    	try{
    		
    		Advertisement advertisement = advertisementService.load(Advertisement.class, id);
    		advertisementService.update(advertisement.pass(principal.getName()));
    		
        	result.success().message("审核成功");
    	}catch(Exception e){
    		result.fail().message("审核失败");
    	}
    	return JsonUtils.toJsonString(result);
    }
    
    /**
     * display the unpass panel
     * 
     * @param id
     * @return the view to confirm the submit
     */
    @RequestMapping("/{id}/unpass")
    public ModelAndView unpass(@PathVariable("id") int id) {
    	ModelAndView modelAndView = new ModelAndView("advertisement/audit/advertisement-audit-unpass");
		modelAndView.addObject("id", id);
		return modelAndView;
    }
    
    /**
     * do unpass
     * 
     * @param id
     * @return the audit result with message
     */
    @RequestMapping(value ="/{id}/unpass", method = RequestMethod.PATCH )
    @MenuSetting("submenu-advertisement-audit")
    public @ResponseBody String onUnpass(@PathVariable("id") int id, @Secure Principal principal) {
     	Result result = new Result();
    	
     	try{
    		
     		Advertisement advertisement = advertisementService.load(Advertisement.class, id);
     		advertisementService.update(advertisement.unpass(principal.getName()));
    		
         	result.success().message("审核成功");
     	}catch(Exception e){
     		result.fail().message("审核失败");
     	}
     	return JsonUtils.toJsonString(result);
    }
}
